<template>
  <div ref="el" :class="className" :style="{ height, width }"></div>
</template>

<script setup lang="ts">
import { onMounted, onBeforeUnmount, ref, nextTick } from 'vue'
import * as echarts from 'echarts'
import { debounce } from 'lodash-es'

// 引入 macarons 主题
import 'echarts/theme/macarons'

// 定义组件名称
defineOptions({
  name: 'BarChart',
})

// 定义组件属性
interface Props {
  className?: string
  width?: string
  height?: string
}

const props = withDefaults(defineProps<Props>(), {
  className: 'chart',
  width: '100%',
  height: '300px',
})

// 图表实例
const chart = ref<echarts.ECharts | null>(null)
const animationDuration = 6000

// 获取 DOM 元素的引用
const el = ref<HTMLDivElement | null>(null)

// Resize 相关逻辑
let sidebarElm: HTMLElement | null = null
let resizeHandler: (() => void) | null = null

const $_sidebarResizeHandler = (e: TransitionEvent) => {
  if (e.propertyName === 'width' && resizeHandler) {
    resizeHandler()
  }
}

const initListener = () => {
  resizeHandler = debounce(() => {
    resize()
  }, 100)
  window.addEventListener('resize', resizeHandler)

  sidebarElm = document.getElementsByClassName('sidebar-container')[0] as HTMLElement
  if (sidebarElm)
    sidebarElm.addEventListener('transitionend', $_sidebarResizeHandler as EventListener)
}

const destroyListener = () => {
  if (resizeHandler) {
    window.removeEventListener('resize', resizeHandler)
    resizeHandler = null
  }

  if (sidebarElm) {
    sidebarElm.removeEventListener('transitionend', $_sidebarResizeHandler as EventListener)
    sidebarElm = null
  }
}

const resize = () => {
  if (chart.value) {
    chart.value.resize()
  }
}

// 初始化图表
const initChart = () => {
  if (el.value) {
    chart.value = echarts.init(el.value, 'macarons')

    chart.value.setOption({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          // 坐标轴指示器，坐标轴触发有效
          type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
        },
      },
      grid: {
        top: 10,
        left: '2%',
        right: '2%',
        bottom: '3%',
        containLabel: true,
      },
      xAxis: [
        {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: 'value',
          axisTick: {
            show: false,
          },
        },
      ],
      series: [
        {
          name: 'pageA',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [79, 52, 200, 334, 390, 330, 220],
          animationDuration,
        },
        {
          name: 'pageB',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [80, 52, 200, 334, 390, 330, 220],
          animationDuration,
        },
        {
          name: 'pageC',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [30, 52, 200, 334, 390, 330, 220],
          animationDuration,
        },
      ],
    })
  }
}

// 生命周期钩子
onMounted(() => {
  nextTick(() => {
    initChart()
    initListener()
  })
})

onBeforeUnmount(() => {
  if (!chart.value) {
    return
  }
  chart.value.dispose()
  chart.value = null
  destroyListener()
})
</script>
